<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/*
 * get 不需要param参数
 * */
function api($url,$param=[],$method='GET',$header=[]){
    $opts=[
        //return string
        CURLOPT_RETURNTRANSFER=>TRUE,
        //connect time wuxian
        CURLOPT_CONNECTTIMEOUT=>0,
        // execute time 30s
        CURLOPT_TIMEOUT=>30,
        // no echo  header
        CURLOPT_HEADER=>FALSE,
        // avoid ssl verify  peer
        CURLOPT_SSL_VERIFYPEER=>FALSE,
        // avoid ssl hostname
        CURLOPT_SSL_VERIFYHOST=>FALSE,
        // set header info
        CURLOPT_HTTPHEADER=>$header
    ];
    switch (strtoupper($method)) {
        case 'GET':
//            $opts[CURLOPT_URL]=$url.'?'.http_build_query($param);
            $opts[CURLOPT_URL]=$url;
            break;
        case 'POST':
            $opts[CURLOPT_URL]=$url;
            $opts[CURLOPT_POST]=TRUE;
            //post fields
            $opts[CURLOPT_POSTFIELDS]=json_encode($param) ;
            break;
    }
    $ch=curl_init();
    curl_setopt_array($ch, $opts);
    $result=curl_exec($ch);
    curl_close($ch);
    return $result;
}